limsPreFolder.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-pre-folder-edit-list-grid',
            url : limsPreFolder.baseUrl + '/queries/raw',
            requestData : {
                status_EQ : 'draft',
                addInBatch_EQ: '0'
            },
            page : true,
            service : 'limsPreFolderServiceImpl',
            dbTable : 'T_LIMS_PRE_FOLDER',
            columns : limsPreFolder.getEditListColumns(),
            onCellClick: function (field, row) {
                if (field === 'batchCode' && row.batchId) {
                    Gikam.loadBatchDetailPageV12(row.batchId, true)
                }
            },
            onUpdated: function (data) {
                if (!Gikam.isEmpty(data.etCode)) {
                    this.refreshRowById(data.id);
                }
            },
            deleteFormatter : function(row) {
                return row.sampleCode || row.id;
            },
            toolbar : [ /*{
                type: 'button',
                text: 'T_LIMS_PRE_FOLDER.BUTTON.CONFIRM',
                icon: 'submit',
                onClick : function() {
                    let grid = Gikam.getComp('lims-pre-folder-edit-list-grid');
                    let selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                        return;
                    }
                    if (grid.validate(selections)) {
                        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.TIP.SUBMIT', function () {
                            workspace.window.showMask();
                            Gikam.postText(limsPreFolder.baseUrl + '/submit',
                                Gikam.getJsonWrapper(null, [null, selections])).done(function () {
                                grid.refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            });
                        });
                    }
                }
            },*/ {
                type: 'button',
                text: 'T_LIMS_PRE_FOLDER.BUTTON.HISTORY',
                icon: 'select',
                onClick : function() {
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig('pre-folder-choose').title,
                        url: limsPreFolder.baseUrl + '/page/history-choose-list' + Gikam.param({
                            status_EQ: 'done',
                            // type_EQ: 'N',
                        }),
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            workspace.window.showMask();
                            Gikam.postText(limsPreFolder.baseUrl + '/issue/historical-task', Gikam.getJsonWrapper(null, [null, data])).done(function () {
                                Gikam.getComp('lims-pre-folder-edit-list-grid').refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            });
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'T_LIMS_PRE_FOLDER.BUTTON.MIX',
                icon: 'select',
                onClick : function() {
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig('mix-task-choose').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig('mix-task-choose').url,
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            workspace.window.showMask();
                            Gikam.postText(limsPreFolder.baseUrl + '/issue/mix-task', Gikam.getJsonWrapper(null, [null, data])).done(function () {
                                Gikam.getComp('lims-pre-folder-edit-list-grid').refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            });
                        }
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.STOP',
                icon : 'remove-row',
                onClick : function() {
                    let selections = Gikam.getComp('lims-pre-folder-edit-list-grid').getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                        return;
                    }
                    Gikam.confirm('GIKAM.BUTTON.STOP', 'GIKAM.TIP.CONFIRM_TO_STOP', function () {
                        workspace.window.showMask();
                        Gikam.postText(limsPreFolder.baseUrl + '/stop',
                            Gikam.getJsonWrapper(null, [null, selections])).done(function (result) {
                            Gikam.getComp('lims-pre-folder-edit-list-grid').refresh();
                        }).always(function () {
                            workspace.window.closeMask();
                        });
                    })
                    // for (var i = 0; i < selections.length; i++) {
                    //     if (selections[i].type === 'N') {
                    //         Gikam.alert('T_LIMS_PRE_FOLDER.DELETE.TIP.MESSAGE');
                    //         return;
                    //     }
                    // }
                    // Gikam.getComp('lims-pre-folder-edit-list-grid').deleteRows(limsPreFolder.baseUrl);
                }
            }, {
                type: 'button',
                text: 'T_LIMS_PRE_FOLDER.BUTTON.CHANGE_TEMPLATE',
                icon: 'edit',
                onClick : function() {
                    let selections = Gikam.getComp('lims-pre-folder-edit-list-grid').getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig("product-template-choose").title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("product-template-choose").url + Gikam.param({
                            single: true,
                        }),
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = selections.map(function (item) {
                                    return {
                                        id: item.id,
                                        productEtId: rows[0].id,
                                        etId: rows[0].etId,
                                        etCode: rows[0].etCode,
                                    }
                                });
                                var idList = selections.map(function (item) {
                                    return item.id
                                });
                                workspace.window.showMask()
                                Gikam.put(limsPreFolder.baseUrl + '/instant', Gikam.getJsonWrapper(null, [null, chooseData])).done(function () {
                                    Gikam.getComp('lims-pre-folder-edit-list-grid').refreshRowById(idList);
                                }).always(function () {
                                    workspace.window.closeMask();
                                    Gikam.getLastModal().close(data);
                                })
                            }
                        }
                    })
                }
            },{
                type: 'button',
                text: 'T_LIMS_SAMPLE.SAMPLECODE',
                icon: 'select',
                onClick: function () {
                    var mainGrid = Gikam.getComp('lims-pre-folder-edit-list-grid')
                    Gikam.createLimsModal({
                        title: 'T_CORE_CHART.SEARCHRULE',
                        height: '300',
                        width: '400',
                        onConfirm: function () {
                            var form = Gikam.getComp('sampleCode-form-prefolder');
                            var data = form.getData()
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            Gikam.postText(limsPreFolder.baseUrl + '/queries/raw', Gikam.getJsonWrapper({
                                f: {
                                    sampleCode_IN: data.sampleCode.replace(/[(\r\n)|(\ +)|(，)|(；)|(;)]/g, ','),
                                    status_EQ : 'draft',
                                    addInBatch_EQ: '0'
                                },
                                n : -1
                            })).done(function (data) {
                                data = JSON.parse(data)
                                mainGrid.loadData(data.rows)
                            }).always(function () {
                                Gikam.getLastModal().close()
                            });
                        },
                        m : {
                            type: 'form',
                            id: 'sampleCode-form-prefolder',
                            columns: 1,
                            titleWidth: 120,
                            fields: [{
                                field: 'sampleCode',
                                title: 'T_LIMS_SAMPLE.SAMPLECODE',
                                type: 'textarea',
                                colspan: 2,
                                height: 180,
                            }],
                        }
                    })
                }
            },{
                type: 'button',
                text: 'T_LIMS_PRE_FOLDER.BUTTON.SEPARATESAMPLE',//分样
                icon: 'edit',
                onClick: function () {
                    let selections = Gikam.getComp('lims-pre-folder-edit-list-grid').getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                        return;
                    }
                    Gikam.createLimsModal({
                        title: 'T_LIMS_PRE_FOLDER.BUTTON.SEPARATESAMPLE',
                        height: '200',
                        width: '300',
                        onConfirm: function () {
                            var form = Gikam.getComp('separateSample-form-prefolder');
                            var data = form.getData()
                            if (Gikam.isEmpty(data)) {
                                return;
                            }
                            Gikam.postText(limsPreFolder.baseUrl + '/separateSample', Gikam.getJsonWrapper({
                                splitNum : data.separateSampleNum,splitFlag : data.splitFlag }, [null, selections])).done(function () {
                                Gikam.getComp('lims-pre-folder-edit-list-grid').refresh();
                            }).always(function () {
                                Gikam.getLastModal().close()
                            });
                        },
                        m : {
                            type: 'form',
                            id: 'separateSample-form-prefolder',
                            columns: 1,
                            titleWidth: 100,
                            fields: [{
                                field: 'separateSampleNum',
                                title: 'T_LIMS_PRE_FOLDER.SEPARATESAMPLENUM',//数量
                                validators: ['notEmpty', 'number', 'numRange(0,)'],
                            },{
                                field : 'splitFlag',
                                title : 'T_LIMS_PRE_FOLDER.SPLITFLAG',
                                value : '1',
                                type : 'simpleCheckbox'
                            }],
                        }
                    })
                }
            }/*, {
                type: 'button',
                text: 'LIMS.BUTTON.SAMPLE_SEARCH',
                icon: 'preview',
                onClick : function() {
                }
            } */]
        }
    },

    getIssuedParam: function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-operation-batch-edit-list-grid',
            url : limsPreFolder.operationBatchUrl + '/queries/raw',
            requestData : {
                status_EQ : 'draft',
                createdById_EQ: workspace.user.id,
            },
            // page: true,
            service : 'limsOperationBatchServiceImpl',
            dbTable : 'T_LIMS_OPERATION_BATCH',
            columns : limsPreFolder.getOperationBatchColumns(),
            deleteFormatter : function(row) {
                return row.operationBatch;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    _this.loadInfoList();
                }
            },
            onRowActive : function(index, row) {
                _this.loadInfoList(row);
            },
            toolbar : [ {
                type: 'button',
                text: 'T_LIMS_OPERATION_BATCH.BUTTON.CREATE_RUN',
                icon: 'select',
                onClick : function() {
                    _this.addBatch();
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    _this.delBatch();
                }
            }, {
                type : 'button',
                text : 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon : 'submit', color: 'success',
                onClick : function() {
                    _this.submitBatch();
                }
            } ]
        }
    },

    loadInfoList : function(row) {
        var _this = this;
        // 下发批次详情信息
        Gikam.getAsyncComp('lims-pre-task-supplement-edit-list-grid').done(function (grid) {
            if (row) {
                grid.setOptions({
                    url: limsPreFolder.baseUrl + '/issue/sample/queries',
                    requestData: {
                        operationBatch_EQ : row.operationBatch,
                        status_EQ : 'draft',
                        addInBatch_EQ: '1'
                    }
                });
                _this.refreshGridColumns(grid, row.operationBatch);
                grid.refresh();
            } else {
                grid.cleanData();
                grid.options.columns = limsPreFolder.getPreTaskSupplementColumns();
                grid.refreshColumns(limsPreFolder.getPreTaskSupplementColumns());
            }
        });
    },

    refreshGridColumns: function (grid, operationBatch) {
        var _this = this;
        var url = Gikam.printf(limsPreFolder.preTaskSupplementUrl + '/{operationBatch}/supplement-to-columns/queries', {
            operationBatch: operationBatch
        });
        limsPreFolder.supplementColumns = limsPreFolder.getPreTaskSupplementColumns();
        Gikam.postSync(url).done(function (data) {
            if (Gikam.isNotEmpty(data)) {
                Gikam.each(data.rows, function (index, item) {
                    var fieldName = item.fieldName;
                    limsPreFolder.supplementColumns.push({
                        field: 'ext$.' + fieldName,
                        title: item.unit ? fieldName + '('+ item.unit +')' : fieldName,
                        validators: (item.requiredFlag === '1' || item.requiredflag === '1') ? ['notEmpty'] : null,
                        editor: true,
                        width: 120,
                        sort: false,
                        autoSave: false,
                        onChange: function (field, value, index) {
                            _this.saveSupplementValue(fieldName, value, this.getData()[index].id, function () {
                                grid.refreshRowByIndex(index);
                            });
                        },
                        onBeforeEditorRender: function (row) {
                            return row.ext$.supplementinfo && eval('(' + row.ext$.supplementinfo + ')').hasOwnProperty(fieldName);
                        },
                        styleFormatter: function (row) {
                            return {
                                // backgroundColor: (row.ext$.supplementinfo && eval('(' + row.ext$.supplementinfo + ')')[fieldName])? '#7CFC00': null
                            }
                        }
                    });
                });
            }
        });
        grid.cleanData();
        grid.options.columns = limsPreFolder.supplementColumns;
        grid.refreshColumns(limsPreFolder.supplementColumns);
    },

    saveSupplementValue: function (fieldName, value, preFolderId, callback) {
        var url = Gikam.printf(limsPreFolder.preTaskSupplementUrl + '/{preFolderId}/update/supplement', {
            preFolderId: preFolderId
        });
        workspace.window.showMask();
        Gikam.postText(url, Gikam.getJsonWrapper(null, [null, [{
            fieldValue: value,
            fieldName: fieldName

        }]])).done(function (data) {
            callback && callback();
        }).always(function () {
            workspace.window.closeMask();
        });
    },

    getIssuedInfoParam: function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'lims-pre-task-supplement-edit-list-grid',
            // page: true,
            service : 'limsPreFolderServiceImpl',
            columns : limsPreFolder.getPreTaskSupplementColumns(),
            toolbar : [ {
                type: 'button',
                text: 'T_LIMS_ORDER_TASK.BUTTON.ADD_IN_RUN',
                icon: 'add',
                onClick : function() {
                    _this.addToBatch();
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick : function() {
                    _this.removeFromBatch();
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EXPORT',
                icon: 'export',
                onClick: function () {
                    var issuedGrid = Gikam.getComp('lims-operation-batch-edit-list-grid')
                    var actRow = issuedGrid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                        return false
                    }
                    Gikam.getText(IFM_CONTEXT + '/core/module/item/files/template-urls' + Gikam.param({
                        templateCode: 'LimsPreTaskSupplementExportTemplate',
                        service: 'LimsPreTaskSupplementExportTemplate',
                        id: actRow.operationBatch
                    })).done(function (url) {
                        Gikam.download(url)
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.IMPORT',
                icon: 'upload',
                onClick: function () {
                    var issuedGrid = Gikam.getComp('lims-operation-batch-edit-list-grid')
                    var actRow = issuedGrid.getActivedRow()
                    if (Gikam.isEmpty(actRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                        return false
                    }
                    Gikam.create('simpleUploader', {
                        dbTable: 'T_LIMS_PRE_FOLDER_SUPPLEMENT',
                        bizId: 'self',
                        autoClose: true,
                        multiple: false,
                        accept: ['xls'],
                        onAfterClose: function (fields) {
                            if (Gikam.isNotEmpty(fields)) {
                                workspace.window.showMask()
                                let url = Gikam.printf(IFM_CONTEXT + '/core/module/item/files/{id}/action/import/' + 'LimsPreTaskSupplementImportFileFunction', {
                                    id: fields[0].id
                                }) + Gikam.param({operationBatch: actRow.operationBatch})
                                Gikam.post(url).done(req => {
                                    Gikam.getComp('lims-pre-task-supplement-edit-list-grid').refresh()
                                    if (req && req.message) {
                                        Gikam.alert(req.message)
                                    }
                                    if (req.url){
                                        Gikam.download(req.url);
                                    }
                                }).always(function () {
                                    workspace.window.closeMask()
                                })
                            }
                        }
                    })
                }
            }]
        }
    },

    addBatch: function() {
        let grid = Gikam.getComp('lims-pre-folder-edit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        if (!grid.validate(selections)) return;
        var etCode = selections[0].etCode;
        if (selections.filter(s => s.etCode !== etCode).length > 0) {
            Gikam.alert('T_LIMS_OPERATION_BATCH.TIP.CHOOSE_SAME_TEMPLATE');
            return;
        }
        workspace.window.showMask();
        Gikam.postText(limsPreFolder.operationBatchUrl + '/operationBatch/create',
            Gikam.getJsonWrapper(null, [null, selections])).done(function () {
            grid.refresh();
            Gikam.getComp('lims-operation-batch-edit-list-grid').refresh();
        }).always(function () {
            workspace.window.closeMask();
        });
    },

    delBatch: function() {
        let grid = Gikam.getComp('lims-operation-batch-edit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        workspace.window.showMask();
        Gikam.postText(limsPreFolder.operationBatchUrl + '/operationBatch/delete',
            Gikam.getJsonWrapper(null, [null, selections])).done(function () {
            grid.refresh();
            Gikam.getComp('lims-pre-folder-edit-list-grid').refresh();
        }).always(function () {
            workspace.window.closeMask();
        });
    },

    addToBatch : function(){
        var _this = this;
        let grid = Gikam.getComp('lims-pre-folder-edit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        let batchGrid = Gikam.getComp('lims-operation-batch-edit-list-grid');
        let activedRow = batchGrid.getActivedRow();
        if (Gikam.isEmpty(activedRow)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
            return;
        }
        if (!grid.validate(selections)) return;
        var etCode = activedRow.etCode;
        if (selections.filter(s => s.etCode !== etCode).length > 0) {
            Gikam.alert('T_LIMS_OPERATION_BATCH.TIP.CHOOSE_SAME_TEMPLATE');
            return;
        }
        workspace.window.showMask();
        Gikam.postText(limsPreFolder.baseUrl + '/operationBatch/add/tasks',
            Gikam.getJsonWrapper({
                operationBatch : activedRow.operationBatch
            }, [null, selections])).done(function () {
            grid.refresh();
            _this.loadInfoList(activedRow);
        }).always(function () {
            workspace.window.closeMask();
        });
    },

    removeFromBatch : function(){
        var _this = this;
        let grid = Gikam.getComp('lims-pre-task-supplement-edit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        workspace.window.showMask();
        Gikam.postText(limsPreFolder.baseUrl + '/operationBatch/remove/tasks',
            Gikam.getJsonWrapper(null, [null, selections])).done(function () {
            _this.loadInfoList(Gikam.getComp('lims-operation-batch-edit-list-grid').getActivedRow());
            Gikam.getComp('lims-pre-folder-edit-list-grid').refresh();
        }).always(function () {
            workspace.window.closeMask();
        });
    },

    submitBatch: function() {
        let grid = Gikam.getComp('lims-operation-batch-edit-list-grid');
        let selections = grid.getSelections();
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
            return;
        }
        Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.TIP.SUBMIT', function () {
            workspace.window.showMask();
            Gikam.postText(limsPreFolder.baseUrl + '/submit/operationBatch',
                Gikam.getJsonWrapper(null, [null, selections])).done(function () {
                grid.refresh();
            }).always(function () {
                workspace.window.closeMask();
            });
        });
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-pre-folder-edit-list-layout',
            renderTo : workspace.window.$dom,
            north: {
                height: '50%',
                items : [ this.getGridParam() ]
            },
            center : {
                items: [{
                    type: 'layout',
                    west: {
                        width: '30%',
                        title: 'T_LIMS_OPERATION_BATCH.TITLE.RUN',
                        items: [ _this.getIssuedParam() ]
                    },
                    center: {
                        title: 'T_LIMS_OPERATION_BATCH.TITLE.SAMPLE',
                        items: [ _this.getIssuedInfoParam() ]
                    }
                }]
            }
        });
    },

    init : function() {
        this.create();
    }
};
